package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.vo.ChapterVO;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zr
 * @since 2021-11-12
 */
@RestController
@RequestMapping("/edu/service/chapter")
@Api(tags = "课程章节管理")
//@CrossOrigin //跨域
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程Id显示 章节信息
     *
     * @param courseId
     * @return
     */
    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("getChapterVideo/{courseId}")
    public Result getChapterVideo(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        List<ChapterVO> chapterVOList = eduChapterService.getChapterVideoByCourseId(courseId);
        return Result.ok(chapterVOList);
    }

    // 添加章节
    @ApiOperation(value = "新增章节")
    @PostMapping("addChapter")
    public Result addChapter(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return Result.ok();
    }

    // 根据Id进行查询章节
    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("getChapterInfo/{chapterId}")
    public Result getChapterInfoById(
            @ApiParam(name = "chapterId", value = "章节ID", required = true)
            @PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return Result.ok(eduChapter);
    }

    // 修改章节
    @ApiOperation(value = "根据ID修改章节")
    @PostMapping("updateChapter")
    public Result updateChapter(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    // 根据Id删除章节  这个地方得考虑到 章节下是否有小节
    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("deleteChapter/{chapterId}")
    public Result deleteChapter(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String chapterId) {
        boolean flag = eduChapterService.deleteChapter(chapterId);
        return flag ? Result.ok() : Result.fail();
    }
}

